<?php
			
	/* SVN FILE: $Id: index.php 62 2011-01-24 11:42:23Z andreoletti $ */
	
	/**
	* Project Name : arcadia
	*
	* @author $Author: andreoletti $
	* @version $Revision: 62 $
	* @lastrevision $Date: 2011-01-24 12:42:23 +0100 (lun, 24 gen 2011) $
	* @filesource $URL: http://andreoletti@www.fletchlynd.com/arcadia_svn/trunk/budget/index.php $
	*/

	require_once 'include/functions.php';
	require_once 'include/const.inc.php';
	require_once '../include/auth.inc.php';
	require_once '../include/conn.inc.php';

	require_once('../include/table.php');

	$sHeadContent .= fnHeadContent($sTable);
	
	require_once('../themes/'.$sTHEME.'/template.php');
	die();
	
	function fnHeadContent($sTable) {
	
		global $sSUITE_NAME;
		global $sAPP;
		
		global $sJsVariables;
		global $sJsDocumentReady;
		global $sDrawCallback;
		global $sColumnDefs;
		global $sSorting;
		global $iDisplayLength;
		global $iDisplayStart;
		global $sAoDataPush;

		$sHeadContent = <<<EOT

		<script type="text/javascript">

		$(document).ready(function(){
			
			$sJsVariables
			
			$sJsDocumentReady			
			
		   /**
			* thousandSeparator2 (NUMBER n, STRING sep) STRING
			* or
			* thousandSeparator2 (STRING n, STRING sep) STRING
			*
			* Convert a number to the format xxx.xxx.xxx,xxxx
			* Accepts integers, floating point or string
			*
			* Does not accept exponential notation, etc.
			*
			* n – the number to be formatted
			* sep – the separator character. if skipped, “,” is used
			*/
			
			function thousandSeparator2 (n, sep) {
				var sRegExp = new RegExp('(-?[0-9]+)([0-9]{3})');
				sValue = n.replace ('.', ',') + '';
				if (sep === undefined) { sep=','; }
				while(sRegExp.test(sValue)) {
					sValue = sValue.replace(sRegExp, '$1'+sep+'$2');
				}
				return sValue;
			}
			

			var oSummary = $("#summary-table").dataTable( {	
				"fnFooterCallback": function ( nRow, aaData, iStart, iEnd, aiDisplay ) {
					var iTotal = 0;
					for ( var i=0 ; i<aaData.length ; i++ ) {
						iTotal += aaData[i][2] * 1;
					}
					// Modify the footer row to match what we want
					var nCells = nRow.getElementsByTagName('th');
					if ((iTotal.toFixed(2) * 100) == (iTotal.toFixed(0) * 100))
						iTotal = iTotal.toFixed(0);
					else 
						iTotal = iTotal.toFixed(2);
					nCells[1].innerHTML = thousandSeparator2(iTotal, '.');
				},	
				"aoColumnDefs": [  { "bVisible": false, "aTargets": [ 2 ] },  { "bVisible": true, "aTargets": [ 0, 1 ] },  { "sWidth": "18%", "aTargets": [ 1 ] } ],
				"bAutoWidth": false,	
				"bPaginate": false,
				"bFilter": false,
				"bSort": false,
				"bInfo": false,
				"bProcessing": false,
				"bServerSide": true,
				"sAjaxSource": "./json.php",
				"fnServerData": function (sSource, aoData, fnCallback ) {
					// Add some extra data to the sender
					aoData.push( 	$sAoDataPush
									{ "name": "sWhat", "value": "summary-table" },
									{ "name": "sTable", "value": "$sTable" } );
					$.getJSON(sSource, aoData, function (json) { 
						// Do whatever additional processing you want on the callback, then tell DataTables
						fnCallback(json)
					});
				},
				
				"fnDrawCallback": function() {

					$('#summary-table tbody tr').each( function () {
						$(this).addClass('gradeA');
					});
					
					$('.summary-link').click(function() {
						$('#'+$(this).attr('field')+' option[value='+$(this).attr('value')+']').attr('selected', 'selected');
						$('#'+$(this).attr('field')).trigger('change');
					});

				}
				
			});

			
			var oTable = $('#main-table').dataTable({
				"sDom": 'rt<"dt_info"ip><"dt_length"l>',
				"oLanguage": { "sUrl": "../include/dataTables/it_IT.txt" },		
				"aoColumnDefs": [ $sColumnDefs ],
				"bAutoWidth": false,
				"aaSorting": $sSorting,
				"iDisplayLength": $iDisplayLength,
				"iDisplayStart": $iDisplayStart,
				"sPaginationType": "full_numbers",
				"bProcessing": true,
				"bServerSide": true,
				"sAjaxSource": "./json.php",
				"fnServerData": function (sSource, aoData, fnCallback ) {
					/* Add some extra data to the sender */
					aoData.push( 	$sAoDataPush
									{ "name": "sWhat", "value": 'main-table' },
									{ "name": "sTable", "value": "$sTable" } );
					$.getJSON(sSource, aoData, function (json) { 
						/* Do whatever additional processing you want on the callback, then tell DataTables */
						fnCallback(json)
					});
				},
				
				"fnDrawCallback": function() {
					
					$sDrawCallback
					
					$('.tooltip', oTable.fnGetNodes()).cluetip({
				    	showTitle: false,
				      	cluetipClass: 'jtip', 
				      	arrows: true, 
				      	dropShadow: false
					});
					
					// interessante spostare questa funzione insieme a tags
					$('.draggable').draggable({
						revert: 'invalid', // when not dropped, the item will revert back to its initial position
						scroll: true
					});
					
					$(".prettydate").prettyDate();

				}
				
			});

		});

	</script>
EOT;

	return $sHeadContent;

	}
?>